<?php

/**
 * @Author: king
 * @Date:   2019-05-04 18:03:07
 * @Last Modified by:   king
 * @Last Modified time: 2019-05-12 12:18:01
 */
namespace App\Models\Api;
use App\Exceptions\ApiException;
use App\Models\Api\Base;
use Cache;
/**
 * 新订单模型
 * Class LanmeiOrder
 * @package app\common\model
 */
class LanmeiOrder extends Base
{
    protected $table = 'astro_order';

    const TAG = ':order';

    /**
     * 订单列表相关缓存,有分页
     */
    const USER_ALL_OIDS = ':user:all:oids';//用户所有订单id
    const USER_UNDERWAY_OIDS = ':user:underway:oids';//用户进行中订单id
    const USER_COMPLETE_OIDS = ':user:complete:oids';//用户已完成订单id
    const USER_UNPAID_OIDS = ':user:unpaid:oids';//用户未支付或待支付的订单id

    const RID_PREPAID_FORTUNE = ':rid:prepaid:fortune';//当前档案已解锁的运势时间
    const RID_UNPAID_FORTUNE = ':rid:unpaid:fortune';//当前档案未支付的运势时间
    const RID_PREPAID_MAP = ':rid:prepaid:map';//当前档案已解锁的命格线
    const RID_UNPAID_MAP = ':rid:unpaid:map';//当前档案未支付的命格线
    const USER_PREPAID_VIDEO = ':user:prepaid:video';//当前用户已解锁的课程
    const USER_UNPAID_VIDEO = ':user:unpaid:video';//当前用户未支付的课程
    const ORDER_INFO = 'order:info:';//订单详情
    const CACHE_TIME = 86400 * 7;

    protected $OrderType;
    protected $OrderStatus;

    public function __construct()
    {
        parent::__construct();
        $this->OrderType = config('zhunleorder.order_type');
        $this->OrderStatus = config('zhunleorder.order_status');
    }

    /**
     * 清除订单缓存
     */
    public function rm_cache()
    {

    }

    /**
     *订单入库相关模型
     * @param array $map
     * @param int $id
     * @return bool
     */
    public function saveOrder($map = [], $id = 0)
    {
        if ($id === 0) {
            try {

                $oid = $this->insertGetId($map);

            } catch (\Exception $e) {
                throw new ApiException('下单失败,请稍候重试', config('code.error'));
            }
        } else {
            try {

                $oid = $this->update($map, ['id' => $id]);

            } catch (\Exception $e) {
               throw new ApiException('订单信息更新失败', config('code.error'));
            }
        }

        return $oid;
    }

    /**
     * 获取用户所有订单的信息和id
     * @param array $map
     * @return array|bool
     */
    public function getUserAllOrder($map = [])
    {
        $page = $map['page'];
        $uid = $map['uid'];

        $orders = false;

        if ($this->cache_on >= 1 && Cache::has($uid . self::USER_ALL_OIDS)) {
            $orders = Cache::get($uid . self::USER_ALL_OIDS);
        }

        if ($orders === false) {
            try {
                $where = ['status' => 0, 'uid' => $uid];
                $orders = $this->where($where)->select('id','order_type')->get();
                // 将订单id组成json字符串
                $orders = json_encode($orders, true);

                if ($this->cache_on >= 1) {
                    Cache::tags($uid . self::TAG)->set($uid . self::USER_ALL_OIDS, $orders, self::CACHE_TIME);
                }
            } catch (\Exception $e) {
                throw new ApiException('获取用户订单失败', config('code.error'));
            }
        }

        $data_list = $this->logical_process(['orders' => $orders, 'page' => $page]);
        return $data_list;
    }


    /**
     * 获取用户进行中订单的信息和id(生时矫正,运势,退款中)
     * @param array $map
     * @return array|bool
     */
    public function getUserUnderWayOrder($map = [])
    {
        $page = $map['page'];

        $uid = $map['uid'];

        $orders = false;

        if ($this->cache_on >= 1 && Cache::has($uid . self::USER_UNDERWAY_OIDS)) {
            $orders = Cache::get($uid . self::USER_UNDERWAY_OIDS);
        }

        if ($orders === false) {
            //获取进行中的运势订单id
            try {
                $where = ['o.status' => 0, 'o.uid' => $uid];
                $order_status = $this->OrderStatus['underway_order'];
                $fortune_type = $this->OrderType['fortune'];
                $rec_type = $this->OrderType['rec'];
                $orders = $this
                    ->from('astro_order as o')
                    ->select('o.id','o.order_type')
                    ->join('astro_order_goods as og', 'o.id','=','og.oid', 'INNER')
                    ->join('astro_fortune as f', 'o.id','=','f.oid', 'LEFT')
                    ->join('rectification as f', 'o.reid','=','re.id', 'LEFT')
                    ->where($where)
                    ->whereRaw("o.pay_status=$order_status OR (o.order_type=$fortune_type ADN f.fortune_status=0) OR (o.order_type=$rec_type AND re.rec_status=0)")
                    ->select();
                // 将订单id组成json字符串
                $orders = json_encode($orders, true);

                if ($this->cache_on >= 1) {
                    Cache::tags($uid . self::TAG)->set($uid . self::USER_UNDERWAY_OIDS, $orders, self::CACHE_TIME);
                }
            } catch (\Exception $e) {
                throw new ApiException('获取用户订单失败', config('code.error'));
            }
        }

        $data_list = $this->logical_process(['orders' => $orders, 'page' => $page]);
        return $data_list;
    }

    /**
     * 获取用户已完成订单的信息和id(生时矫正,运势,订单)
     * @param array $map
     * @return array|bool
     */
    public function getUserCompleteOrder($map = [])
    {
        $page = $map['page'];

        $uid = $map['uid'];

        $orders = false;

        if ($this->cache_on >= 1 && Cache::has($uid . self::USER_COMPLETE_OIDS)) {
            $orders = Cache::get($uid . self::USER_COMPLETE_OIDS);
        }

        if ($orders === false) {
            //获取进行中的运势订单id
            try {
                $where = ['o.status' => 0, 'o.uid' => $uid];
                $order_status = $this->OrderStatus['complete_order'];
                $fortune_type = $this->OrderType['fortune'];
                $rec_type = $this->OrderType['rec'];
                $orders = $this
                    ->from('astro_order as o')
                    ->select('o.id','o.order_type')
                    ->join('astro_order_goods as og', 'o.id','=','og.oid', 'INNER')
                    ->join('astro_fortune as f', 'o.id','=','f.oid', 'LEFT')
                    ->join('rectification as re', 'o.reid','=','re.id', 'LEFT')
                    ->where($where)
                    ->whereRaw("o.pay_status in ('$order_status') OR (o.order_type=$fortune_type AND f.fortune_status=1) OR (o.order_type=$rec_type AND re.rec_status in ('1,2'))")
                    ->get();
                // 将订单id组成json字符串
                $orders = json_encode($orders, true);

                if ($this->cache_on >= 1) {
                    Cache::tags($uid . self::TAG)->set($uid . self::USER_COMPLETE_OIDS, $orders, self::CACHE_TIME);
                }
            } catch (\Exception $e) {
                throw new ApiException('获取用户订单失败', config('code.error'));
            }
        }

        $data_list = $this->logical_process(['orders' => $orders, 'page' => $page]);
        return $data_list;
    }


    /**
     * 获取用户未支付的订单的信息和id
     * @param array $map
     * @return array|bool
     */
    public function getUserUnpaidOrder($map = [])
    {
        $page = $map['page'];
        $uid = $map['uid'];

        $orders = false;

        if ($this->cache_on >= 1 && Cache::has($uid . self::USER_UNPAID_OIDS)) {
            $orders = Cache::get($uid . self::USER_UNPAID_OIDS);
        }

        if ($orders === false) {
            try {
                $order_status = $this->OrderStatus['unpaid_order'];
                $where = ['status' => 0, 'uid' => $uid, 'pay_status' => $order_status];
                $orders = $this->where($where)->select('id','order_type')->get();
                // 将订单id组成json字符串
                $orders = json_encode($orders, true);

                if ($this->cache_on >= 1) {
                    Cache::tags($uid . self::TAG)->set($uid . self::USER_UNPAID_OIDS, $orders, self::CACHE_TIME);
                }
            } catch (\Exception $e) {
                throw new ApiException('获取用户订单失败', config('code.error'));
            }
        }

        $data_list = $this->logical_process(['orders' => $orders, 'page' => $page]);
        return $data_list;
    }


    /**
     * 获取订单信息相关逻辑处理
     * @param array $map
     * @return array
     */
    public function logical_process($map = [])
    {
        $orders = $map['orders'];
        $page = $map['page'];

        $orders = json_decode($orders, true);
        $per_page = 10;
        $total = count($orders);
        $data_list = getPaginatorData($total, $page, $per_page);

        //获取截取后的订单id
        $oids = array_slice($orders, $data_list['start'], $per_page);
        unset($data_list['start']);
        unset($data_list['end']);

        // 根据当前页订单id获取所需的订单信息
        if (!empty($oids)) {
            $rec_oids = '';
            $fortune_oids = '';
            $all_oids = '';
            $temp = [];

            // 从缓存订单内容,根据订单id
            foreach ($oids as $ac_id) {
                if ($this->cache_on >= 1 && Cache::has(self::ORDER_INFO . $ac_id['id'])) {
                    // 根据订单id
                    $temp[$ac_id['id']] = Cache::get(self::ORDER_INFO . $ac_id['id']);
                    if ($temp[$ac_id['id']]) {
                        $arr = true;
                    } else {
                        // 缓存未开启时，默认从数据库获取
                        $arr = false;
                    }
                    // 订单内容未缓存，等待从数据库中获取
                    if ($ac_id['order_type'] == $this->OrderType['rec'] && $arr === false && $ac_id['id']) {
                        $rec_oids .= ',' . $ac_id['id'];
                    } elseif ($ac_id['order_type'] == $this->OrderType['fortune'] && $arr === false && $ac_id['id']) {
                        $fortune_oids .= ',' . $ac_id['id'];
                    } elseif ($arr === false && $ac_id['id']) {
                        $all_oids .= ',' . $ac_id['id'];
                    }
                }

            }

            // 从数据库获取订单内容
            //获取生时矫正订单信息
            if ($rec_oids != '' || strlen($rec_oids) > 0) {
                // 去掉开头的,
                $ac_ids = substr($rec_oids, 1);

                $rec = $this->getRecOrderInfo(['oids' => $ac_ids]);
            } else {
                $rec = [];
            }

            //获取运势订单信息
            if ($fortune_oids != '' || strlen($fortune_oids) > 0) {
                // 去掉开头的,
                $ac_ids = substr($fortune_oids, 1);

                $fortune = $this->getFortuneOrderInfo(['oids' => $ac_ids]);
            } else {
                $fortune = [];
            }

            //获取统一订单信息
            if ($all_oids != '' || strlen($all_oids) > 0) {
                // 去掉开头的,
                $ac_ids = substr($all_oids, 1);

                $all_order = $this->getALLOrderInfo(['oids' => $ac_ids]);
            } else {
                $all_order = [];
            }

            $data_list['$data'] = array_merge($temp, $rec, $fortune, $all_order);

            foreach ($temp as $item) {
                if ($item) {
                    $data_list['data'][] = $item;
                }
            }
        }

        return $data_list;
    }


    /**
     * 获取生时矫正订单详情,根据订单id
     * @param array $map
     * @return array|bool
     */
    public function getRecOrderInfo($map = [])
    {
        $oids = $map['oids'];
        $temp = [];

        // 根据当前页订单id获取所需的订单信息
        if (!empty($oids)) {

            $field = ['gct.id as gid','gct.name as gname','gct.or_price','gct.now_price','gct.attr','or.id','or.id','or.order_type','or.status','or.pay_status','or.order_id','or.pay_type_id','or.total_amount','or.create_time','or.pay_time','or.return_time','re.id as reid','re.rec_status','re.start_time','re.end_time','og.rid'];
            try {
                $where = ['or.status' => 0, 're.status' => 0];
                $orders = $this->from('astro_order as o')
                    ->select($field)
                    ->join('astro_order_goods as og', 'or.id','=','og.oid', 'INNER')
                    ->join('astro_goods_commodity_attr as gct', 'og.gctid','=','gct.id', 'INNER')
                    ->join('rectification as re', 'or.reid','=','re.id', 'LEFT')
                    ->where($where)
                    ->whereIn('or.id' ,$oids)
                    ->orderBy('or.create_time', 'DESC')
                    ->get();

                $orders = json_decode(json_encode($orders, true), true);

            } catch (\Exception $e) {
               throw new ApiException('获取生时矫正订单信息失败', config('code.error'));
            }

            if (!empty($orders)) {
                foreach ($orders as $c) {
                    $arr[$c['id']][] = $c;
                    // 将订单内容缓存
                    if ($this->cache_on >= 1) {
                        Cache::tags(self::TAG)->set(self::ORDER_INFO . $c['id'], $c, self::CACHE_TIME);
                        Cache::tags(self::TAG)->set(self::ORDER_INFO . $c['order_id'], $c, self::CACHE_TIME);
                    }
                    // 将数据库查出来的内容 写入到数组中为空的部分
                    if ($c) {
                        $temp[$c['id']] = $c;
                    }
                }
            }

        }

        return $temp;
    }

    /**
     * 获取运势订单详情
     * @param array $map
     * @return array|bool
     */
    public function getFortuneOrderInfo($map = [])
    {
        $oids = $map['oids'];

        $temp = [];

        if (!empty($oids)) {
            $field = ['gct.id as gid','gct.name as gname','gct.or_price','gct.now_price','gct.attr','or.id','og.rid','or.id','or.order_type','or.status','or.pay_status','or.order_id','or.pay_type_id','or.total_amount','or.create_time','or.pay_time','or.return_time','f.time','f.type','f.fortune_status'];

            try {
                $where = ['or.status' => 0];
                $orders = $this->from('astro_order as o')
                    ->select($field)
                    ->join('astro_order_goods as og', 'or.id','=','og.oid', 'INNER')
                    ->join('astro_goods_commodity_attr as gct', 'og.gctid','=','gct.id', 'INNER')
                    ->join('astro_fortune as f', 'or.id','=','f.oid', 'LEFT')
                    ->where($where)
                    ->whereIn('or.id',$oids)
                    ->orderBy('or.create_time', 'DESC')
                    ->get();

                $orders = json_decode(json_encode($orders, true), true);

            } catch (\Exception $e) {
                throw new ApiException('获取运势订单信息失败', config('code.error'));
            }

            if (!empty($orders)) {
                foreach ($orders as $c) {
                    $arr[$c['id']][] = $c;
                    // 将订单内容缓存
                    if ($this->cache_on >= 1) {
                        Cache::tags(self::TAG)->set(self::ORDER_INFO . $c['id'], $c, self::CACHE_TIME);
                        Cache::tags(self::TAG)->set(self::ORDER_INFO . $c['order_id'], $c, self::CACHE_TIME);
                    }
                    // 将数据库查出来的内容 写入到数组中为空的部分
                    if ($c) {
                        $temp[$c['id']] = $c;
                    }
                }
            }

        }
        return $temp;

    }

    /**
     * 获取统一订单详情
     * @param array $map
     * @return array
     */
    public function getAllOrderInfo($map = [])
    {
        $oids = $map['oids'];
        $temp = [];

        $field = ['gct.id as gid','gct.name as gname','gct.or_price','gct.now_price','gct.attr','or.id','or.order_type','or.status','or.pay_status','or.order_id','or.pay_type_id','or.total_amount','or.create_time','or.pay_time','or.return_time','og.rid'];

        try {
            $where = ['or.status' => 0];
            $orders = $this->from('astro_order as o')
                ->select($field)
                ->join('astro_order_goods as og', 'or.id','=','og.oid', 'INNER')
                ->join('astro_goods_commodity_attr as gct', 'og.gctid','=','gct.id', 'INNER')
                ->where($where)
                ->whereIn('or.id',$oids)
                ->orderBy('or.create_time', 'DESC')
                ->get();

            $orders = json_decode(json_encode($orders, true), true);
        } catch (\Exception $e) {
            throw new ApiException('获取占星地图订单信息失败', config('code.error'));
        }

        if (!empty($orders)) {
            foreach ($orders as $c) {
                // 将订单内容缓存
                if ($this->cache_on >= 1) {
                    Cache::tags(self::TAG)->set(self::ORDER_INFO . $c['id'], $c, self::CACHE_TIME);
                    Cache::tags(self::TAG)->set(self::ORDER_INFO . $c['order_id'], $c, self::CACHE_TIME);
                }
                // 将数据库查出来的内容 写入到数组中为空的部分
                if ($c) {
                    $temp[$c['id']] = $c;
                }
            }
        }

        return $temp;
    }

    /**
     * 获取订单详情信息
     * @param array $map
     * @return array|bool|mixed
     */
    public function getOrderinfo($map = [])
    {
        $order_type = $map['order_type'];
        $oid = $map['oid'] ?? 0;
        $order_id = $map['order_id'] ?? '';

        $data = false;
        $arr = [];

        if ($oid) {
            $id = $oid;
            $where = ['or.id' => $oid];
        } else {
            $id = $order_id;
            $where = ['or.order_id' => $order_id];
        }

        if ($this->cache_on >= 1 && Cache::has(self::ORDER_INFO . $id)) {
            $data = Cache::get(self::ORDER_INFO . $id);
        }


        if ($data === false) {
            switch ($order_type) {
                case 1:
                    $field = ['gct.id as gid','gct.name as gname','gct.or_price','gct.now_price','gct.attr','or.id','or.order_type','or.status','or.pay_status','or.order_id','or.pay_type_id','or.total_amount','or.create_time','or.pay_time','or.return_time','re.id as reid','re.rec_status','re.start_time','re.end_time','og.rid'];
                    try {
                        $where['or.status'] = 0;
                        $where['re.status'] = 0;
                        $data = $this->from('astro_order as o')
                            ->select($field)
                            ->join('astro_order_goods as og', 'or.id','=','og.oid', 'INNER')
                            ->join('astro_goods_commodity_attr as gct', 'og.gctid','=','gct.id', 'INNER')
                            ->join('rectification as re', 'or.reid','=','re.id', 'LEFT')
                            ->where($where)
                            ->orderBy('or.create_time', 'DESC')
                            ->get();
                        $data = json_decode(json_encode($data, true), true);
                    } catch (\Exception $e) {
                        throw new ApiException('获取订单信息失败', config('code.error'));
                    }
                    break;
                case 2:
                    $field = ['gct.id as gid','gct.name as gname','gct.or_price','gct.now_price','gct.attr','og.rid','or.id','or.order_type','or.status','or.pay_status','or.order_id','or.pay_type_id','or.total_amount','or.create_time','or.pay_time','or.return_time','f.time','f.type','f.fortune_status'];

                    try {
                        $where['or.status'] = 0;
                        $data = $this->from('astro_order as o')
                            ->select($field)
                            ->join('astro_order_goods as og', 'or.id','=','og.oid', 'INNER')
                            ->join('astro_goods_commodity_attr as gct', 'og.gctid','=','gct.id', 'INNER')
                            ->join('astro_fortune as f', 'or.id','=','f.oid', 'LEFT')
                            ->where($where)
                            ->orderBy('or.create_time', 'DESC')
                            ->get();
                        $data = json_decode(json_encode($data, true), true);
                    } catch (\Exception $e) {
                        throw new ApiException('获取订单信息失败', config('code.error'));
                    }
                    break;
                default :
                    $field = ['gct.id as gid','gct.name as gname','gct.or_price','gct.now_price','gct.attr','or.id','or.order_type','or.status','or.pay_status','or.order_id','or.pay_type_id','or.total_amount','or.create_time','or.pay_time','or.return_time','og.rid'];

                    try {
                        $where['or.status'] = 0;
                        $data = $this->from('astro_order as o')
                            ->select($field)
                            ->join('astro_order_goods as og', 'or.id'.'=','og.oid', 'INNER')
                            ->join('astro_goods_commodity_attr as gct', 'og.gctid','=','gct.id', 'INNER')
                            ->where($where)
                            ->orderBy('or.create_time', 'DESC')
                            ->get();
                        $data = json_decode(json_encode($data, true), true);
                    } catch (\Exception $e) {
                        throw new ApiException('获取订单信息失败', config('code.error'));
                    }
            }

            if (!empty($data)) {
                foreach ($data as $c) {
                    $arr[] = $c;
                }
                // 将订单内容缓存
                if ($this->cache_on >= 1) {
                    Cache::tags(self::TAG)->set(self::ORDER_INFO . $id, $arr, self::CACHE_TIME);
                }
            }
        }
        return $data;
    }

    /**
     * 获取当前档案解锁的运势时间(日运,月运,年运)
     * @param array $map
     * @return array|bool|mixed
     */
    public function getRidprepaidFortune($map = [])
    {
        $uid = $map['uid'];
        $rid = $map['rid'];
        $fortune_type = $map['fortune_type'];
        $order_type = $map['order_type'];
        $data = false;

        if ($this->cache_on >= 1 && Cache::has($rid . self::RID_PREPAID_FORTUNE)) {
            $data = Cache::get($rid . self::RID_PREPAID_FORTUNE);
        }
        if ($data === false) {
            $where = ['o.order_type' => $order_type, 'o.pay_status' => 1, 'og.rid' => $rid, 'f.type' => $fortune_type];
            try {
                $data = $this
                    ->from('astro_order as o')
                    ->where($where)
                    ->join('astro_order_goods as og', 'o.id','=','og.oid', 'INNER')
                    ->join('astro_fortune as f', 'o.id','=','f.oid', 'INNER')
                    ->get(['f.time']);
                if ($this->cache_on >= 1) {
                    Cache::tags($uid . self::TAG)->set($rid . self::RID_PREPAID_FORTUNE, $data, self::CACHE_TIME);
                }
            } catch (\Exception $e) {
                throw new ApiException('档案运势解锁状态查询失败', config('code.error'));
            }
        }
        return $data;
    }

    /**
     * 获取当前档案未支付的运势时间(日运,月运,年运)
     * @param array $map
     * @return array|bool|mixed
     */
    public function getRidUnpaidFortune($map = [])
    {
        $uid = $map['uid'];
        $rid = $map['rid'];
        $fortune_type = $map['fortune_type'];
        $order_type = $map['order_type'];
        $data = false;

        if ($this->cache_on >= 1 && Cache::has($rid . self::RID_UNPAID_FORTUNE)) {
            $data = Cache::get($rid . self::RID_UNPAID_FORTUNE);
        }
        if ($data === false) {
            $where = ['o.order_type' => $order_type, 'o.pay_status' => 0, 'o.status' => 0, 'og.rid' => $rid, 'f.type' => $fortune_type];
            try {
                $data = $this
                    ->from('astro_order as o')
                    ->select('o.id','f.time')
                    ->where($where)
                    ->join('astro_order_goods as og', 'o.id','=','og.oid', 'INNER')
                    ->join('astro_fortune as f', 'o.id','=','f.oid', 'INNER')
                    ->get();
                $data = json_encode($data, true);
                if ($this->cache_on >= 1) {
                    Cache::tags($uid . self::TAG)->set($rid . self::RID_UNPAID_FORTUNE, $data, self::CACHE_TIME);
                }
            } catch (\Exception $e) {
                throw new ApiException('档案运势解锁状态查询失败', config('code.error'));
            }
        }
        $data = json_decode($data, true);
        return $data;
    }

    /**
     * 获取当前档案已支付的命格线
     * @param array $map
     * @return array|bool|mixed
     */
    public function getRidPrepaidMap($map = [])
    {
        $uid = $map['uid'];
        $rid = $map['rid'];
        $order_type = $map['order_type'];
        $data = false;

        if ($this->cache_on >= 1 && Cache::has($rid . self::RID_PREPAID_MAP)) {
            $data = Cache::get($rid . self::RID_PREPAID_MAP);
        }
        if ($data === false) {
            $where = ['o.order_type' => $order_type, 'o.pay_status' => 1, 'og.rid' => $rid];
            try {
                $data = $this
                    ->from('astro_order as o')
                    ->where($where)
                    ->join('astro_order_goods as og', 'o.id','=','og.oid', 'INNER')
                    ->join('rectification as re', 'o.reid','=','re.id', 'INNER')
                    ->get(['og.gctid']);
                if ($this->cache_on >= 1) {
                    Cache::tags($uid . self::TAG)->set($rid . self::RID_PREPAID_MAP, $data, self::CACHE_TIME);
                }
            } catch (\Exception $e) {
                throw new ApiException('档案命格线解锁状态查询失败', config('code.error'));
            }
        }
        return $data;
    }

    /**
     * 获取当前档案未支付的命格线订单
     * @param array $map
     * @return array|bool|mixed
     */
    public function getRidUnpaidMap($map = [])
    {
        $uid = $map['uid'];
        $rid = $map['rid'];
        $order_type = $map['order_type'];
        $data = false;

        if ($this->cache_on >= 1 && Cache::has($rid . self::RID_UNPAID_MAP)) {
            $data = Cache::get($rid . self::RID_UNPAID_MAP);
        }
        if ($data === false) {
            $where = ['o.order_type' => $order_type, 'o.pay_status' => 0, 'o.status' => 0, 'og.rid' => $rid];
            try {
                $data = $this
                    ->from('astro_order as o')
                    ->select('o.id','og.gctid')
                    ->where($where)
                    ->join('astro_order_goods as og', 'o.id','=','og.oid', 'INNER')
                    ->join('rectification as re', 'o.reid','=','re.id', 'INNER')
                    ->get();
                $data = json_encode($data, true);
                if ($this->cache_on >= 1) {
                    Cache::tags($uid . self::TAG)->set($rid . self::RID_UNPAID_MAP, $data, self::CACHE_TIME);
                }
            } catch (\Exception $e) {
               throw new ApiException('档案命格线解锁状态查询失败', config('code.error'));
            }
        }
        $data = json_decode($data, true);
        return $data;
    }

    /**
     * 获取用户解锁的课程
     * @param array $map
     * @return bool
     */
    public function getUserPrepaidVideo($map = [])
    {
        $uid = $map['uid'];
        $order_type = $map['order_type'];
        $data = false;

        if ($this->cache_on >= 1 && Cache::has($uid . self::USER_PREPAID_VIDEO)) {
            $data = Cache::get($uid . self::USER_PREPAID_VIDEO);
        }

        if ($data === false) {
            $where = ['o.order_type' => $order_type, 'o.pay_status' => 1, 'o.uid' => $uid];
            try {
                $data = $this
                    ->from('astro_order as o')
                    ->join('astro_order_goods as og', 'o.id','=','og.oid', 'INNER')
                    ->where($where)
                    ->get(['og.gctid']);
                if ($this->cache_on >= 1) {
                    Cache::tags($uid . self::TAG)->set($uid . self::USER_PREPAID_VIDEO, $data, self::CACHE_TIME);
                }
            } catch (\Exception $e) {
                throw new ApiException('用户课程解锁状态查询失败', config('code.error'));
            }
        }

        return true;
    }

    /**
     * 获取用户未支付的课程信息
     * @param array $map
     * @return bool|false|mixed|\PDOStatement|string|\think\Collection
     */
    public function getUserUnpaidVideo($map = [])
    {
        $uid = $map['uid'];
        $order_type = $map['order_type'];
        $data = false;

        if ($this->cache_on >= 1 && Cache::has($uid . self::USER_PREPAID_VIDEO)) {
            $data = Cache::get($uid . self::USER_PREPAID_VIDEO);
        }

        if ($data === false) {
            $where = ['o.order_type' => $order_type, 'o.pay_status' => 0, 'o.status' => 0, 'o.uid' => $uid];
            try {
                $data = $this
                    ->from('astro_order as o')
                    ->select('o.id','og.gctid')
                    ->join('astro_order_goods as og', 'o.id','=','og.oid', 'INNER')
                    ->where($where)
                    ->get();
                $data = json_encode($data, true);
                if ($this->cache_on >= 1) {
                    Cache::tags($uid . self::TAG)->set($uid . self::USER_PREPAID_VIDEO, $data, self::CACHE_TIME);
                }
            } catch (\Exception $e) {
                throw new ApiException('用户课程解锁状态查询失败', config('code.error'));
            }
        }

        $data = json_decode($data, true);

        return $data;
    }

}
